package com.chaos.auth.service.oauth;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.chaos.auth.api.data.oauth.OauthClientDetailsData;
import com.chaos.auth.entity.OauthClientDetails;
import com.chaos.auth.mapper.OauthClientDetailsMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chaos.framework.model.dto.page.PageList;
import com.chaos.framework.model.dto.page.PageQueryDto;
import com.chaos.framework.model.helper.ConvertHelper;
import com.chaos.framework.model.helper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author Jacky
 * @since 2024-05-07
 */
@Slf4j
@Component
public class OauthClientDetailsService extends ServiceImpl<OauthClientDetailsMapper, OauthClientDetails> {

    public void insertModel(OauthClientDetailsData data) {
        OauthClientDetails entity = ConvertHelper.convert(data, OauthClientDetails.class);
        save(entity);
    }

    public List<OauthClientDetailsData> selectByData(OauthClientDetailsData data) {
        QueryWrapper<OauthClientDetails> query = new QueryWrapper<>();
        query.lambda()
                .eq(!ObjectUtil.isEmpty(data.getClientId()), OauthClientDetails::getClientId, data.getClientId())
                .eq(!ObjectUtil.isEmpty(data.getClientSecret()), OauthClientDetails::getClientSecret, data.getClientSecret())
                .eq(!ObjectUtil.isEmpty(data.getTenantId()), OauthClientDetails::getTenantId, data.getTenantId())
        ;
        return ConvertHelper.convert(list(query), OauthClientDetailsData.class);
    }

    public PageList<OauthClientDetailsData> selectByPage(PageQueryDto<OauthClientDetailsData> pageData) {
        QueryWrapper<OauthClientDetails> query = new QueryWrapper<>();
        return PageHelper.pageList(page(PageHelper.page(pageData), query), OauthClientDetailsData.class);
    }

    public OauthClientDetailsData selectByClientId(String clientId) {
        QueryWrapper<OauthClientDetails> query = new QueryWrapper<>();
        query.lambda()
                .eq(OauthClientDetails::getClientId, clientId);
        OauthClientDetails entity = getOne(query);
        return ConvertHelper.convert(entity, OauthClientDetailsData.class);
    }

    public boolean updateModelByID(OauthClientDetailsData data) {
        OauthClientDetails entity = ConvertHelper.convert(data, OauthClientDetails.class);
        return update(entity, new UpdateWrapper<OauthClientDetails>().lambda()
                .eq(OauthClientDetails::getClientId, data.getClientId()));
    }


    public OauthClientDetailsData selectSaasByData(OauthClientDetailsData data) {
        QueryWrapper<OauthClientDetails> query = new QueryWrapper<>();
        query.lambda()
                .eq(OauthClientDetails::getTenantId, data.getTenantId())
                .like(OauthClientDetails::getAuthorizedGrantTypes, data.getAuthorizedGrantTypes())
        ;
        List<OauthClientDetails> entity = list(query);
        return ConvertHelper.convert(ObjectUtil.isEmpty(entity) ? null : entity.get(0), OauthClientDetailsData.class);
    }


}
